#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int left = 0;
        int right = arr.size() - 1;
        while (left <= right)
        {
            int mid = left + (right - left+1) / 2;
            if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1])
                return mid;
            else if (arr[mid] <= arr[mid - 1])
            {
                right = mid - 1;
            }
            else if (arr[mid] <= arr[mid + 1])
                left = mid + 1;
        }
        return left;
    }
};
int main()
{
    vector<int> arr = { 3,5,3,2,0 };
    Solution s;
    s.peakIndexInMountainArray(arr);

    return 0;
}
//class Solution {
//public:
//    vector<int> searchRange(vector<int>& nums, int target) {
//        if (nums.size() == 0)
//            return { -1,-1 };
//        int left = 0;
//        int begin = -1;
//        int end = -1;
//        int right = nums.size() - 1;
//        while (left < right)
//        {
//            int mid = left + (right - left) / 2;
//            if (nums[mid] < target)
//            {
//                left = mid + 1;
//            }
//            if (nums[mid] >= target)
//                right = mid;
//        }
//        if (nums[left] == target)
//            begin = left;
//        left = 0;
//        right = nums.size() - 1;
//        while (left < right)
//        {
//            int mid = left + (right - left + 1) / 2;
//            if (nums[mid] <= target)
//                left = mid;
//            if (nums[mid] > target)
//                right = mid - 1;
//        }
//        if (nums[left] == target)
//            end = left;
//        return { begin,end };
//    }
//};
//
//
//class Solution {
//public:
//    int searchInsert(vector<int>& nums, int target) {
//        int left = 0;
//        int right = nums.size() - 1;
//        int begin = 0;
//        int end = 0;
//        while (left < right)
//        {
//            int mid = left + (right - left + 1) / 2;
//            if (nums[mid] == target)
//                return mid;
//            else if (nums[mid] <= target)
//                left = mid;
//            else
//                right = mid - 1;
//        }
//        if (nums[left] == target)
//            return left;
//        if (nums[left] > target)
//            return 0;
//        return left + 1;
//    }
//};
//
//class Solution {
//public:
//    int mySqrt(int x) {
//        int left = 0;
//        int right = x;
//        while (left < right)
//        {
//            int mid = left + (right - left) / 2;
//            if (pow(mid, 2) < x)
//                left = mid + 1;
//            else
//                right = mid;
//        }
//        if (pow(right, 2) <= x)
//            return right;
//        return right - 1;
//    }
//};

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[mid + 1])
                right = mid;
            else
                left = mid + 1;
        }
        return left;
    }
};